package com.bw.controller;

import cn.hutool.core.util.NumberUtil;
import com.bw.common.TokenUtils;
import com.bw.entity.*;
import com.bw.mapper.ApplyMapper;
import com.bw.service.ApplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class ApplyController {

    @Autowired
    ApplyMapper applyMapper;


    @Autowired
    ApplyService applyService;

    /**
     * 列表页
     * 接收查询请求
     *
     * @param model
     * @return
     */
    @RequestMapping("list.do")
    public String list(ApplyQuery query, Integer pageNum, Model model, HttpServletRequest request) {
        // 列表页展示用户昵称头像
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);

        model.addAttribute("nickName",userFromRequest.getNickName());
        model.addAttribute("avatarUrl",userFromRequest.getAvatarUrl());

        //渲染下拉框
        List<Dict> typeList = applyMapper.listType();
        model.addAttribute("typeList", typeList);

        // 执行查询  分页
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);
        // 开启分页之后 ，下一个SQL语句必须是查列表
        List<ApplyInfo> list = applyMapper.selectByParam(query);
        PageInfo<ApplyInfo> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfo", pageInfo);

        // 查询条件回显
        model.addAttribute("query", query);
        return "list";
    }

    /**
     * 完成报销
     * @param id
     * @return
     * 参数绑定 ，客户端发送参数名  和入参的参数名一致
     */
    @RequestMapping("/complete.do")
    public String complete(Integer id){

        applyMapper.complete(id);

        return "redirect:list.do";
    }

    /**
     * 添加页面
     * @param model
     * @return
     */
    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        // 渲染单选框
        List<Dict> typeList = applyMapper.listType();
        model.addAttribute("typeList", typeList);

        return "add";
    }


    /**
     * 执行添加
     * @param model
     * @return
     */
    @RequestMapping("/add.do")
    public String add(@Valid ApplyAdd applyAdd, BindingResult result, Model model){
        // 非空验证
        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            // 重新渲染下拉框
            List<Dict> typeList = applyMapper.listType();
            model.addAttribute("typeList", typeList);
            return "add";
        }
        // 执行添加 报销金额支持小数点2位

        String amount = applyAdd.getAmount();
        // 保留两位小数
        BigDecimal bigDecimal = NumberUtil.round(amount, 2);
        applyAdd.setAmount(bigDecimal.toString());

        applyService.add(applyAdd);

        return "redirect:list.do";
    }


    @RequestMapping("/del.do")
    public String del(String id){
        System.out.println("批量删除的ID：" + id);
        applyMapper.delById(id);
        return "redirect:list.do";
    }
}
